package org.easy.core.util;

/**
 * 线程栈格式化打印
 */
public class ThreadStackExtractUtil {

    /**
     * 获取当前线程调用栈
     *
     * @return
     */
    public static String extracts() {
        return ThreadStackExtractUtil.extracts(Thread.currentThread().getStackTrace());
    }

    /**
     * 获取指定线程调用栈
     *
     * @return
     */
    public static String extracts(StackTraceElement[] elements) {
        StringBuffer stringBuffer = new StringBuffer();
        for (StackTraceElement element : elements) {
            stringBuffer.append(extractSingle(element)).append("\r\n");
        }
        return stringBuffer.toString();

    }

    /**
     * 获取单个线程调用栈
     *
     * @return
     */
    public static String extractSingle(StackTraceElement element) {
        if (element.getFileName() == null) {
            return element.toString();
        }
        return element.getFileName().replace(".java", "") + "." + element.getMethodName() + "():" + element.getLineNumber();
    }


}
